<template>
    <!-- 新增/修改弹出窗口 -->
    <el-dialog :title="isEdit ?'编辑资产'+form.assetNo :'新增资产信息'" :visible.sync="visible" :close-on-click-modal="false"
        @close="onClose">
        <!-- <h5 v-if="!isEdit">新增资产信息</h5>
        <h5 v-else>编辑资产: {{ form.assetNo }}</h5> -->
        <el-form :model="form" ref="form" label-width="100px" :rules="rules" size="mini" :inline="true">
            <el-scrollbar class="menu-scrollbar" max-height="300px" height="300px">
                <el-form-item label="资产编号" prop="assetNo">
                    <el-input v-model="form.assetNo" :disabled="isEdit"></el-input>
                </el-form-item>
                <!-- 新增表单项 -->
                <el-form-item label="营运公司编号">
                    <el-input v-model="form.omCompanyId"></el-input>
                </el-form-item>
                <el-form-item label="公司名称">
                    <el-input v-model="form.companyName"></el-input>
                </el-form-item>
                <el-form-item label="资产名称">
                    <el-input v-model="form.assetName"></el-input>
                </el-form-item>
                <el-form-item label="资产规格">
                    <el-input v-model="form.assetSpec"></el-input>
                </el-form-item>
                <el-form-item label="资产类别名称">
                    <el-input v-model="form.assetCategoryName"></el-input>
                </el-form-item>
                <el-form-item label="资产性质">
                    <el-input v-model="form.assetNature"></el-input>
                </el-form-item>
                <el-form-item label="计量模式">
                    <el-input v-model="form.countWay"></el-input>
                </el-form-item>
                <el-form-item label="资产状态">
                    <el-input v-model="form.assetStatus"></el-input>
                </el-form-item>
                <el-form-item label="单据类型名称">
                    <el-input v-model="form.docTypeName"></el-input>
                </el-form-item>
                <el-form-item label="摊提方法">
                    <el-input v-model="form.amortizationMethod"></el-input>
                </el-form-item>
                <el-form-item label="取得时间">
                    <el-date-picker v-model="form.getDate" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="记账时间">
                    <el-date-picker v-model="form.bookkeepingDate" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="期间">
                    <el-date-picker v-model="form.period" type="month" placeholder="选择月份" format="yyyy年MM月"
                        value-format="yyyy-M"></el-date-picker>
                </el-form-item>
                <el-form-item label="开始摊提日期">
                    <el-date-picker v-model="form.startAmortizationDate" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="处置/转换日期">
                    <el-date-picker v-model="form.handleConversionDate" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="摊提状态">
                    <el-input v-model="form.amortizationStatus"></el-input>
                </el-form-item>
                <el-form-item label="责任部门名称">
                    <el-input v-model="form.responsibilityDepartmentName"></el-input>
                </el-form-item>
                <el-form-item label="来源类型">
                    <el-input v-model="form.sourceType"></el-input>
                </el-form-item>
                <el-form-item label="来源单据">
                    <el-input v-model="form.sourceDocNo"></el-input>
                </el-form-item>
                <el-form-item label="来源序号">
                    <el-input v-model="form.sourceSeq"></el-input>
                </el-form-item>
                <el-form-item label="来源日期">
                    <el-date-picker v-model="form.sourceDate" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="状态">
                    <el-input v-model="form.approveStatus"></el-input>
                </el-form-item>
                <el-form-item label="审核日期">
                    <el-date-picker v-model="form.approveDate" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="创建者编号">
                    <el-input v-model="form.createUserNo" :disabled="isEdit"></el-input>
                </el-form-item>
                <el-form-item label="创建者姓名">
                    <el-input v-model="form.createUserName" :disabled="isEdit"></el-input>
                </el-form-item>
                <el-form-item label="创建日期">
                    <el-date-picker v-model="form.createDate" type="date" placeholder="选择日期"
                        :disabled="isEdit"></el-date-picker>
                </el-form-item>
                <el-form-item label="最后修改人编号">
                    <el-input v-model="form.lastModifyUserNo" :disabled="isEdit"></el-input>
                </el-form-item>
                <el-form-item label="最后修改人姓名">
                    <el-input v-model="form.lastModifyUserName" :disabled="isEdit"></el-input>
                </el-form-item>
                <el-form-item label="最后修改日期">
                    <el-date-picker v-model="form.lastModifyDate" :disabled="isEdit" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="修改人编号">
                    <el-input v-model="form.modifyUserNo" :disabled="isEdit"></el-input>
                </el-form-item>
                <el-form-item label="修改人姓名">
                    <el-input v-model="form.modifyUserName" :disabled="isEdit"></el-input>
                </el-form-item>
                <el-form-item label="修改日期">
                    <el-date-picker v-model="form.modifyDate" :disabled="isEdit" type="date" placeholder="选择日期"
                        value-format="yyyy-MM-dd"></el-date-picker>
                </el-form-item>
                <el-form-item label="是否为历史数据" prop="history">
                    <el-switch v-model="form.history" active-text="是" inactive-text="否" active-value="Y"
                        inactive-value="N"></el-switch>
                </el-form-item>
            </el-scrollbar>
            <el-form-item size="large" style="width:100%;text-align: center;">
                <el-button type="primary" @click="onSubmit" :disabled="okDisabled">{{ btnOkText }}</el-button>
                <el-button @click="onClose" :disabled="cancelDisabled">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import request from '@/utils/request'

export default {
    name: "addtionDialog",
    props:{
        formData: {
            type: Object,
            default: () => {
                return {}
            }
        }
        ,dialogVisible: {
            type: Boolean,
            default: false
        }
    }
    ,data() {
        return {
            form: {}
            ,isEdit: true
            ,rules: {
                assetNo: [
                    { required: true, message: '资产编号不能为空。', trigger: 'blur' }
                ]
                , history: [
                    { required: true, message: '是否为历史数据不能为空。', trigger: 'blur' }
                ]
            }
            ,visible:this.dialogVisible
            , btnOkText:'新增'
            , okDisabled:false
            , cancelDisabled:false
            ,isDataModify:false
        }
    }
    , methods: {
        onClose(){
            this.visible = false;
            this.$emit('close',this.isDataModify);
        }
       ,onSubmit(){
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submitRequest().then(res => {
                       if(res.code === 200){
                           this.isDataModify = true;
                            this.$modal.msgSuccess(this.isEdit?'编辑成功':'新增成功');
                            this.onClose();
                       }else{
                            this.$modal.msgError(res.msg);
                       }
                    }).catch(err => {
                        this.$modal.msgError(err); 
                    }).finally(()=>{
                        this.toggleButtons(); 
                    })
                } else {
                    this.$modal.msgError("请输入必填项。");
                }
            });
        }
        ,submitRequest(){
            this.toggleButtons();
            if (this.isEdit) {
                // 编辑模式
                return request({
                    url: '/asset/editAsset'
                   ,method: 'put'
                   ,data: this.form 
                })
            } else {
                // 新增模式
                return request({
                    url: '/asset/addAsset'
                  ,method: 'post'
                  ,data: this.form 
                })
            }
        }
        ,toggleButtons(){
            this.okDisabled = !this.okDisabled;
            this.cancelDisabled = !this.cancelDisabled;
        }

    }
    , mounted() {
        this.form = JSON.parse(JSON.stringify(this.formData));
        this.isEdit = this.form.assetNo&&this.form.assetNo.length>0;
        if(this.isEdit){
           this.btnOkText = '修改'; 
        }
    }
}
</script>

<style scoped></style>